
# Import the DB modules/methods
import dpcDBs as dbs
from sqlobject import sqlhub
from sqlobject.sqlbuilder import EXISTS, Select

# GUI modules
import gtk
import sqlobject as sqlobject
from dpcGUI import challengeSummary

from dpcDBs.log import log









class previousChallenges( gtk.Window ):


  # Constructor
  def __init__( self ):
    super( previousChallenges, self ).__init__()
    self.set_position( gtk.WIN_POS_CENTER )


    self.connect( "destroy", gtk.main_quit )
    self.set_title( "Previous Challenge Entries" )






    # Icon view for the challenges 
    self.store = gtk.ListStore( gtk.gdk.Pixbuf, str )
    iconView = gtk.IconView( self.store )
    iconView.set_pixbuf_column( 0 )
    iconView.set_text_column( 1 )
    iconView.connect( "item-activated", self.on_activated )


    # The challenge summary object
    self.challengeSummary = challengeSummary();
    self.challengeSummary.hide()


    # The more button
    more = gtk.Button()
    more.set_label( "Load more entries" )
    more.connect( "clicked", self.populate )



    # Set the populate routine 
    self.populate()


    # Set the scrolled view for the icon display
    sw = gtk.ScrolledWindow()
    sw.add( iconView )
    sw.set_size_request( 475, 550 )


    # Set up the table which will have the challenge summary
    table = gtk.Table()
    table.attach( sw,
                  0, 1,
                  0, 1,
                  gtk.SHRINK,
                  gtk.FILL | gtk.EXPAND,
                  10, 10 )
    table.attach( self.challengeSummary,
                  1, 2,
                  0, 1,
                  gtk.FILL | gtk.EXPAND,
                  gtk.FILL | gtk.EXPAND,
                  10, 10 )

    # Add the main container
    self.add( table )

    # Show everything
    table.show()
    self.show_all()

    self.set_modal( True )
    gtk.main()
    self.set_modal( False )






  # This method is called whenever an item is double-clicked. It sets
  # a new ID for the challenge summary so as to update the summary 
  def on_activated( self, widget, item ):
    log.i( self, 'Viewing new item' )
    ch = self.challenges[item[0]]
    self.challengeSummary.reset( ch.user, ch.challengeID )




  # This method is called back when the 'more' button is pressed
  # It loads more entries from the database and plots the challenges summary
  def populate( self, button = None ):
    log.i( self, 'Populating previous challenge entries' )
    self.challenges = dbs.challengeTable.select().orderBy( dbs.challengeTable.q.challengeID ).reversed()
    if self.challenges.count() == 0:
      return


    # Loop over all challenges
    for ch in self.challenges:
      # Generate the pixbuf from memory
      th = ch.entryThumb
      if len( ch.entryThumb ) > len( ch.entryImage ):
        th = ch.entryImage

      # Generate the pixbuf for displaying
      pixBuf = gtk.gdk.PixbufLoader( "jpeg" )
      pixBuf.write( th )
      pixBuf.close()

      # Add the pixbuf to the grid, and add the challenge name
      # and entry name to the thing
      self.store.append( [ pixBuf.get_pixbuf(),
                           str( ch.challengeName + "\n" +
                                ch.entryName ), ] )






if __name__ == "__main__":
  from sqlobject import *

  sqlhub.processConnection = connectionForURI( 'sqlite:../dpchallenge.db' )
  previousChallenges()
  gtk.main()
